﻿using System;
using System.Configuration;

namespace OrbitOne.Elmah.MailNotifier
{
    internal class Configuration : ConfigurationSection
    {
        #region Properties
        [ConfigurationProperty("From", IsRequired = true)]
        public string From
        {
            get { return (string)this["From"]; }
            set { this["From"] = value; }
        }
        [ConfigurationProperty("DailyTo", IsRequired = true)]
        public string DailyTo
        {
            get { return this["DailyTo"].ToString(); }
            set { this["DailyTo"] = value; }
        }
        [ConfigurationProperty("AlertTo", IsRequired = true)]
        public string AlertTo
        {
            get { return this["AlertTo"].ToString(); }
            set { this["AlertTo"] = value; }
        }
        [ConfigurationProperty("ElmahWebSite", IsRequired = true)]
        public string ElmahWebSite
        {
            get { return (string)this["ElmahWebSite"]; }
            set { this["ElmahWebSite"] = value; }
        }

        [ConfigurationProperty("AuthenticatedErrorCount", IsRequired = true)]
        public int AuthenticatedErrorCount
        {
            get { return int.Parse(this["AuthenticatedErrorCount"].ToString()); }
            set { this["AuthenticatedErrorCount"] = value; }
        }

        [ConfigurationProperty("AnonymousErrorCount", IsRequired = true)]
        public int AnonymousErrorCount
        {
            get { return int.Parse(this["AnonymousErrorCount"].ToString()); }
            set { this["AnonymousErrorCount"] = value; }
        }

        public string[] DailyEmails
        {
            get { return DailyTo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); }
        }
        public string []AlertEmails
        {
            get { return AlertTo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); }
        }
        #endregion

        #region Constructor
        public Configuration(string from, string dailyTo, string alertTo, string elmahWebSite)
        {
            this.From = from;
            this.DailyTo = dailyTo;
            this.AlertTo = alertTo;
            this.ElmahWebSite = elmahWebSite;
        }

        public Configuration()
        {

        } 
        #endregion

        #region Static Methods
        public static Configuration Load()
        {
            return (Configuration)ConfigurationManager.GetSection("MailNotifierConfiguration");
        }
        #endregion
    }
}